<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Utils\ModelOperatorTrait;
use App\Exports\WeightdataExport;
use Maatwebsite\Excel\Facades\Excel;

class WeightdataController extends Controller
{

    use ModelOperatorTrait;

    protected function model(){
        return app(\App\Models\Weighdata::class);
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $query = $this->queryModel(request()->input());

        if(isset(request()->start_time)){
            $query = $query->where('system_datetime','>=',request()->start_time);
            $query = $query->where('system_datetime','<=',request()->end_time);
        }

        $query = $query->orderBy('system_datetime','DESC');

        if(request()->has('page')){
            return formatJsonResponse($query->paginate());
        }
        else{
            return formatJsonResponse($query->get());
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        return formatJsonResponse($this->findModel($id));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function export(Request $request){
        return Excel::download(new WeightdataExport($request->all()), '称重记录表'. time() .'.xlsx');
    }

    public function processNumber(){
        return formatJsonResponse(itemOption($this->model()::$PROCESS_NUMBER));
    }

    public function transType(){
        return formatJsonResponse(itemOption($this->model()::$TRANS_TYPE));
    }

    public function transStatus(){
        return formatJsonResponse(itemOption($this->model()::$TRANS_STATUS));
    }

    public function isFinished(){
        return formatJsonResponse(itemOption($this->model()::$IS_FINISHED));
    }

    public function weightStatus(){
        return formatJsonResponse(itemOption($this->model()::$WEIGHT_STATUS));
    }
}
